package com.gisuni.sddc.util;
import java.io.IOException;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class CommConfig 
{
	//易流平台数据获取相关配置
	public static String  AppKey = "ce46959b-de61-4383-91a6-92c463d7e228";    //公钥
	public static String  AppSecret = "72fe71f5-1f0b-48d8-8515-fb30d076c3f9"; //密钥
	public static String  MotionBaseURL = "http://api.e6gps.com/public/v3/Inface/Call";
	public static String  ReportBaseURL = "http://api.e6gps.com/public/v3/StatisticsReport/Call";
	public static String  VehicleMap = "http://api.e6gps.com/public/v3/MapServices/Orientation.aspx?";
	public static String  LoginE6WebGisUrl = "http://login.e6gps.com/Home/LoginWithToken?";
	
	//通行平台数据获取相关配置
	public static String LoginName="ytcdwl";
	public static String LoginPWD="tranda168";	
	public static String GPSAPIUrl1="http://60.195.248.66:89/gpsonline/GPSAPI?";
	public static String GPSAPIUrl="http://115.28.134.68:89/gpsonline/GPSAPI?";
	public static String UID="2235076";
	public static String UKEY="320a7d22daf2068e8d04bdac19cde122";
	
	//地理编码url
	public static String  GeoCoderUrl = "http://service.arcgisonline.cn/geocode/328f1b5fc88f2380202a000b79dd1a62a87525866f0ed656416b53c53bb118f3/single?queryStr=";
	
	//地理编码url
	public static String  GeoCoderUrl2 = "http://121.43.230.128:8080/GT/rest/services/batchservice/batch";
	
	public static String Format = "json";
	
	//总部GPS参数
	public static String GeneralUrl="https://58.215.50.52:38443/openapi/openapi";
	public static String devKey="f26a0fa1ad980fb35bee1c6abc674842";
	public static String GeneralLoginName="tianbin@ytxh";
	public static String GeneralPwd="130316";
	
	//天气Url
	public static String weatherUrl="http://php.weather.sina.com.cn/xml.php?password=DJOYnieT8234jlsK&day=0&city=";
	
	//人员
	public static String peopleUrl ="http://s-mdmpub.shinho.net.cn/GisSfaService/GisSfaService.aspx?method=GetPersonInfo&para1=true";
	
	//易流平台数据获取相关方法
	public static String GetNowTime()
	{
		Date dt=new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowTime="";
		nowTime= df.format(dt);
		return nowTime;
	}
	
	public static String GetSign(Map<String, String> map)
	{
		//将所有的键放在一个collection进行排序，是为了生成签名用到
		ArrayList<String> keyList = new ArrayList<String>();
		for(String dataKey  : map.keySet())
		{
			keyList.add(dataKey);
		}
		
		//对键进行排序
		Collections.sort(keyList);
		
		//根据键再从map当中获取值，拼接成字符串
		StringBuilder  sb = new StringBuilder();
		for(String key : keyList)
		{
			String value = map.get(key);
			if(value !=null && value != "")
			{
				sb.append(key);
				sb.append(value);	
			}
			
		}
		
		return Sign(sb.toString());
	}
	
	 /// <summary>
    /// 签名字符串
    /// </summary>
    /// <param name="pPrestr">需要签名的字符串</param>
    /// <param name="pKey">密钥</param>
    /// <param name="_input_charset">编码格式</param>
    /// <returns>签名结果</returns>
    public static String Sign(String pPrestr)
    {
        pPrestr = AppSecret + pPrestr + AppSecret;
        String md5Str = MD5Util.str2MD5(pPrestr);
        return md5Str.toUpperCase();
    }
    
    public static String GetWeather(String msg) throws ParserConfigurationException, SAXException, IOException{
    	
    	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
        DocumentBuilder builder;
		builder = factory.newDocumentBuilder();
		Document doc = builder.parse(new InputSource(new StringReader(msg)));
		Element root=doc.getDocumentElement();
        NodeList weathers = ((Node) root).getChildNodes();
        JSONObject ret=new JSONObject();
        if (weathers != null) {   
            Node weather = weathers.item(0);
            NodeList list=weather.getChildNodes();
            for (int i = 0; i < list.getLength()-1; i++) {   
                Node book = list.item(i);   
                
                if (book.getFirstChild()==null) continue;
                
                if (book.getNodeName()=="status1")
                {
			    	 ret.put("status1", book.getFirstChild().getNodeValue());
                }
                
                if (book.getNodeName()=="temperature1")
                {
			    	 ret.put("temperature1", book.getFirstChild().getNodeValue());
                }
                
                
                if (book.getNodeName()=="temperature2")
                {
			    	 ret.put("temperature2", book.getFirstChild().getNodeValue());
			    	 break;
                }
            }   
        }
        
        return ret.toString();
    }
}
